#
# Ronin Exploits - A Ruby library for Ronin that provides exploitation and
# payload crafting functionality.
#
# Copyright (c) 2007-2013 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# This file is part of Ronin Exploits.
#
# Ronin is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ronin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ronin.  If not, see <http://www.gnu.org/licenses/>
#

require 'ronin/formatting/text'

module Ronin
  module Exploits
    module Helpers
      #
      # Adds methods to exploits for adding additional padding to date.
      #
      # ## Parameters
      #
      # * `padding` (`'A'`) - Padding string.
      #
      module Padding
        def self.extended(obj)
          obj.instance_eval do
            # String to pad extra space with
            parameter :padding,
                      type: String,
                      default: 'A',
                      description: 'padding string'
          end
        end

        protected

        #
        # Creates padding out to a maximum length, using the `padding`
        # parameter.
        #
        # @param [Integer] max_length
        #   The maximum length to pad out to.
        #
        # @return [String]
        #   A padded string.
        #
        # @example
        #   pad(28)
        #   # => "AAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        #
        def pad(max_length)
          ''.pad(@padding.to_s,max_length)
        end

        #
        # Pads the a string on the right-hand side out to a maximum length,
        # using the `padding` parameter.
        #
        # @param [String] data
        #   The string to add padding to.
        #
        # @param [Integer] max_length
        #   The amount of padding to add.
        #
        # @return [String]
        #   The left-hand side padded string.
        #
        # @example
        #   pad_left("\xff\xff",48)
        #   # => "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\xff\xff"
        #
        def pad_left(data,max_length)
          pad(max_length - data.length) + data
        end

        #
        # Pads the a string on the right-hand side out to a maximum length,
        # using the `padding` parameter.
        #
        # @param [String] data
        #   The string to add padding to.
        #
        # @param [Integer] max_length
        #   The amount of padding to add.
        #
        # @return [String]
        #   The right-hand side padded string.
        #
        # @example
        #   pad_right("\xff\xff",48)
        #   # => "\xff\xffAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        #
        def pad_right(data,max_length)
          data.to_s.pad(@padding,max_length)
        end
      end
    end
  end
end
